<?php
/*******************************************************************************
PHPINDEX ? https://vtens.gitee.io/phpindex/ ? ver:219a
|
|--APACHE 配置修改 (httpd.conf)
|  |--Options Indexes FollowSymLinks (去掉Indexes)
|  |--ErrorDocument 404 /?error
|  |--ErrorDocument 403 /?error
|
|--NGINX 配置修改(nginx.conf)
|  |--autoindex  on; (#注释掉autoindex on;)
|  |--error_page 404 403 /?error;
|
|--文件显示说明
|  |--@开头文件夹, 不显示;
|  |--搜索自动聚焦内容
|  |--域名设置白名单
|  |--空文件夹自动创建index.php
|  |--动态密码登录
|
*******************************************************************************/

//die;
// 初始化
ini_set('date.timezone','PRC');
define('DS',DIRECTORY_SEPARATOR);
$host = $_SERVER['HTTP_HOST'];
$path = str_ireplace(['index.php','//'], ['','/'], htmlentities($_SERVER['REQUEST_URI']));
define('PA', $path);
define('AR', '//' . $host . $path);
define('AP', dirname(__FILE__) . str_ireplace('/', '\\', $path));
//echo '<pre>'; print_r(get_defined_constants(true)['user']);die;

// 框架配置
$pass = '123'.date('N'); //登录密码
//$pass = '123'; //登录密码
$wlist = ['localhost','192.168.11.11']; //访问白名单

// 网页配置
$i['ver'] = 'PI219';
$i['pr'] = '//' . $host . '/@public';
$i['cs']['prism'] = "<link href='{$i['pr']}/prism/prism.css' rel='stylesheet'/>";
$i['js']['prism'] = "<script src='{$i['pr']}/prism/prism.js'></script>";
$i['js']['jquery'] = "<script src='{$i['pr']}/jquery/jquery-3.3.1.min.js'></script>";
$i['js']['jqcode'] = "<script src='{$i['pr']}/jquery/jquery-qrcode.js'></script>";
$i['hd'] = "<!DOCTYPE HTML><html><head><meta charset='UTF-8'><meta http-equiv='x-ua-compatible' content='ie=edge'><meta http-equiv='Cache-Control' content='no-cache'/><meta name='viewport' content='width=device-width'><link rel='icon' href='data:,'><style>*{margin:0;padding:0;border:none;outline:none;box-sizing:border-box}body{background:#f1f1f1;color:#123;text-align:center;min-width:300px;font:12px/1.5 Tahoma,'Microsoft YaHei'}a:link,a:active,a:visited,a{text-decoration:none;color:#123}input[type=submit]{-webkit-appearance:none}</style>";
$i['ft'] = "<style>.foot{margin:30px auto;font-size:18px}.foot a{color:#ddd;text-shadow:1px 1px #fff;}.foot a:hover{color:#bbb}</style><div class='foot'><a href='//vtens.gitee.io/phpindex/' target='_blank'>&copy; {$i['ver']}</a></div></body></html>";
$i['cm'] = 'width:100%;position:absolute;top:45%;left:50%;text-align:center;transform:translate(-50%,-50%);';

// 禁止外网访问
if(!in_array($_SERVER['SERVER_NAME'], $wlist) && !filter_var($host, 275)){header('HTTP/1.1 403 Forbidden');msg('禁止访问',4);}

// 登录界面
if(isset($_POST['pass']) && $pass == $_POST['pass']){setrawcookie('phpindex',md5('tens'.$pass),time()+86400,'/');header('location://' . $host);}
if(!isset($_COOKIE['phpindex']) || md5('tens'.$pass) != $_COOKIE['phpindex']){if(isset($_POST['pass']))echo '<script>alert("密码错误");</script>';
die("{$i['hd']}<style>.login{{$i['cm']}width:95%;top:40%}.login .face{font-size:8em;font-weight:800;color:#123;text-shadow:2px 4px #99c}.login input{text-align:center;width:100%;height:50px;font-size:16px;border-bottom:1px solid #ddd;margin-top:40px}.login input[type=submit]{height:50px;line-height:50px;padding:0;cursor:pointer;background:#04BE02;color:#FFF;font-size:20px;border-radius:3px;letter-spacing:4px}.login input[type=submit]:hover{font-weight:900}</style></head><body><div class='login'><div class='face'>PI</div><form action='//{$host}' method='post'><input type='password' name='pass' placeholder='输入密码' autocomplete='off' autofocus='autofocus'><input type='submit' value='登录'></form></div></body></html>");
}

// 小工具
if(isset($_GET['error'])){error();} //404,403
if(isset($_GET['info'])){info();}
if(isset($_GET['ip'])){ip();}
if(isset($_GET['logout'])){logout();}
if(!empty($_GET['md5'])){die(md5($_GET['md5']));}

// 显示目录
$file = glob('*',GLOB_ONLYDIR);
if(count($file)){
	$dir = '';
	foreach($file as $v){
		if($v[0] == '@')continue; //@开头隐藏
		$v = iconv('GBK', 'UTF-8//IGNORE', $v);
		$dir .= "<a class='l' href='{$v}'>{$v}</a>";
	}
	$site = dirname($_SERVER['PHP_SELF']);
	$site = ($site === '\\') ? $host : $site;
	$prev = $host.substr($site, 0, strripos($site, '/'));	
	die("{$i['hd']}<style>.head{line-height:110px;text-align:center;background:#123;}.head h1 a{display:block;color:#fff;font-size:36px;text-shadow:1px 0 1px #333;overflow:hidden}.head h1 a:hover{color:yellow}.menu{height:50px;line-height:50px;text-align:center;background:#fff;box-shadow:0 1px 1px #ddd;}.menu a{display:inline-block;float:left;width:33.33333%;height:100%;color:#333;font-size:14px}.menu a:hover{border-bottom:2px solid #123;color:#123}.menu a:not(:last-child){border-right:1px solid #eee;}.main a{display:block;height:60px;line-height:60px;margin-top:15px;background:#fff;color:#123;font-size:24px;box-shadow:0 1px 1px #ddd;overflow:hidden}/*.main a:after{content:attr(href);}*/.main a:hover{background:#123;color:#fff;text-shadow:0 0 1px #333;}#search{margin-top:15px;}#search .txt{width:100%;height:48px;line-height:48px;text-align:center;letter-spacing:1px;font:16px/1.5 arial;color:#123;border:none;border-bottom:1px solid #ccc;outline:none;}#logout{display:block;line-height:50px;margin:30px 0;background:#ca1919;color:#fff;text-align:center;font-size:21px;letter-spacing:3px}#logout:hover{font-weight:800}</style></head><body><div class='head'><h1><a href='//{$prev}'>{$site}</a></h1></div><div class='menu'><a href='//{$host}/@tool/'>TOOLS</a><a href='?info'>PHPINFO</a><a href='?ip'>MOBILE</a></div><div id='search'><input type='text' class='txt' placeholder='Search'></div><div class='main'>{$dir}</div><a id='logout' href='//{$host}?logout'>退出</a>{$i['ft']}{$i['js']['jquery']}<script>$('#search .txt').on('input propertychange',function(){search();});$('#search .txt').focus();function search(){txt = $('#search .txt').val().toUpperCase();$('.main .l').removeClass('s');$('.main .l').each(function(k,v){yes = $(this).text().toUpperCase().indexOf(txt);if(yes != '-1')$(this).addClass('s');});$('.main .l').hide();$('.main .s').show();}$(function(){document.onkeydown = function(e){var ev = document.all ? window.event : e;if(ev.keyCode==13){if($('#search .txt').val()==''){location.href=$('.main .l:first').text();}else{location.href = $('.main .s:first').text();}}}});</script>");
}else{
	msg('没有<br/>项目文件夹');
}

// IP地址二维码
function ip(){
	global $i;
	$ip = getip();
	$ip2 = getip2();
	if(!$ip)msg('错误<br/>局域网地址');
	$re = pathinfo($_SERVER['SCRIPT_NAME'])['dirname'];
	die("{$i['hd']}<title>手机扫码浏览</title><style>body{background:#123;color:#fff;}p{font-size:16px;letter-spacing:1px}h1{font-size:2.2em;letter-spacing:3px;}#code{display:block;width:256px;height:256px;margin:20px auto;padding:10px;background:#fff}canvas{margin-left:-1px}</style></head><body><div style='{$i['cm']}'><p>同一局域网内, 手机扫描二维码浏览</p><h1>{$ip}</h1><a id='code' href='./'></a><h1 style='color:#123'>{$ip2}</h1></div>{$i['js']['jquery']}{$i['js']['jqcode']}<script>$('#code').qrcode({width:236,height:236,text:'http://{$ip}{$re}/'});</script></body></html>");
}

// 局域网IP
function getip(){
  return gethostbyname(gethostname());
}

/** 外网IP
* https://ifconfig.co/ip
* http://ip-api.com/json/
**/
function getip2(){
	$str = @file_get_contents('http://pv.sohu.com/cityjson?ie=utf-8', TRUE, NULL, 28);
	return $str = explode('",', $str)[0];
}

// 错误页面
function error(){
	global $i;
	$num = count(explode('\\', explode('htdocs\\', AP)[1], -1)); //目录层级
	
	// note
	$file = AP . '123.ini';
	if(is_file($file)){
		$html = file_get_contents($file);
		$html = charset($html);
		die("{$i['hd']}{$i['cs']['prism']}{$i['js']['prism']}<style>body{font:16px/1.5 Helvetica;background:#272822;}code[class*='language-'],pre[class*='language-']{color:#272822;}pre[class*='language-'].line-numbers{margin:0;border-radius:0;}</style></head><body><h2 style='color:#4c90fb;padding:15px 0;border-bottom:2px solid #333;'>项目说明</h2><pre class='line-numbers'><code class='language-ini'>{$html}</code></pre></body></html>");
	}

	// index+
	if(is_dir(AP) && !is_file(AP.'index.php') && !is_file(AP.'index.html') && $num<3){
		file_put_contents(AP.'index.php', "<?php".PHP_EOL."require(\$_SERVER['DOCUMENT_ROOT'].'/index.php');");
		header('location:'.AR);die;
	}
	
	// 404
	header('HTTP/1.1 404 Not Found');
	msg('404',9);
}

// 退出
function logout(){
	setrawcookie('phpindex','',time()-3600);
	header('location:./');
}

// 提醒文字
function msg($str='404',$size='3'){
	global $i;
	die("{$i['hd']}<style>body{background:#123;}h1{color:#fff;{$i['cm']}font-size:{$size}rem;line-height:1.8;letter-spacing:3px;}</style></head><body><h1 title='&copy; vtens.com'>{$str}</h1></body></html>");
}

// phpinfo
function info(){
	global $i;
	phpinfo();
	die("<meta name='viewport' content='width=device-width,user-scalable=no,initial-scale=1'><style>.h i{text-shadow:0 0 3px #fff}</style>{$i['js']['jquery']}<script>$('.center .h h1').after('<i>{$i['ver']}</i>').siblings('a').attr('href','./')</script>");
}

// charset
function charset($data){
  if(!empty($data)){
    $fileType = mb_detect_encoding($data , array('UTF-8','GBK','LATIN1','BIG5')); 
    if($fileType != 'UTF-8'){$data = mb_convert_encoding($data, 'utf-8', $fileType);}  
  }  
  return $data;
}
################################################################################